package com.model.defaults;

import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.core.hibernate.ExceptionHandler;
import com.core.hibernate.exceptionevent.ExceptionEvent;
import com.model.UserContextName;
import com.model.project.ProjectItem;
import com.model.project.ProjectTask;
import com.model.project.TeamMember;
import com.model.user.User;
import com.model.user.UserCategory;
import com.model.user.UserGroup;
import com.model.user.UserRole;


public class ModelDefaultSamples {

	private static User createUser ( Session session,
	                                 String userName,
	                                 String password,
	                                 UserGroup userGroup,
	                                 UserRole userRole ) {

		User user = new User();
		user.setName(userName);
		user.applyPasswordMD5String(password);
		session.save(user);

		UserCategory userCategory = user.getUserCategory();
		userCategory.setUserGroup(userGroup);
		userCategory.setUserRole(userRole);
		session.save(userCategory);
		return user;

	}



	private static TeamMember createTeamMember ( Session session,
	                                             String userName,
	                                             UserGroup userGroup,
	                                             UserRole userRole ) {

		TeamMember member = new TeamMember();
		member.setName(userName);
		session.save(member);

		// UserCategory userCategory = member.getUserCategory();
		// userCategory.setUserGroup(userGroup);
		// userCategory.setUserRole(userRole);
		// session.save(userCategory);
		return member;

	}



	public static void createSamples ( Session session,
	                                   Map <String, UserContextName> userContexts ) {

		Transaction transaction = session.beginTransaction();

		try {

			UserRole roleRoot = ModelDefaultRoles.createRoleRoot(session, userContexts);
			UserRole roleAdmin = ModelDefaultRoles.createRoleAdmin(session, userContexts);
			UserRole roleDEV = ModelDefaultRoles.createRoleDev(session, userContexts);
			UserRole roleQA = ModelDefaultRoles.createRoleQA(session, userContexts);
			UserRole rolePM = ModelDefaultRoles.createRolePM(session, userContexts);

			UserGroup groupParticipaint = new UserGroup();
			groupParticipaint.setName("participaint");
			session.save(groupParticipaint);


			ModelDefaultSamples.createUser(session, "root", "root", groupParticipaint, roleRoot);
			ModelDefaultSamples.createUser(session, "admin", "root", groupParticipaint, roleAdmin);
			ModelDefaultSamples.createUser(session, "QA #1", "root", groupParticipaint, roleQA);
			ModelDefaultSamples.createUser(session, "QA #2", "root", groupParticipaint, roleQA);
			ModelDefaultSamples.createUser(session, "PM", "root", groupParticipaint, rolePM);
			ModelDefaultSamples.createUser(session, "dev #1", "root", groupParticipaint, roleDEV);
			ModelDefaultSamples.createUser(session, "dev #2", "root", groupParticipaint, roleDEV);
			ModelDefaultSamples.createUser(session, "dev #3", "root", groupParticipaint, roleDEV);
			ModelDefaultSamples.createUser(session, "dev #4", "root", groupParticipaint, roleDEV);
			ModelDefaultSamples.createUser(session, "dev #5", "root", groupParticipaint, roleDEV);


			TeamMember member = ModelDefaultSamples.createTeamMember(session, "rootMember", groupParticipaint, roleAdmin);


			ProjectTask task = new ProjectTask();
			task.setName("Task1");
			task.setDescription("task description");
			//task.getSubTasks().add(subtask);
			//subtask.setParentTask(task);
			task.setCreateDate(java.sql.Date.valueOf( "2010-01-31"));
			session.save(task);
			
			ProjectTask task2 = new ProjectTask();
			task2.setName("Task2");
			task2.setDescription("task description");
			//task.getSubTasks().add(subtask);
			//subtask.setParentTask(task);
			task2.setCreateDate(java.sql.Date.valueOf( "2010-01-31"));
			session.save(task2);

			ProjectItem project = new ProjectItem();
			project.setName("Project1");
			project.getProjectTasks().add(task);
			project.getProjectTasks().add(task2);
			task.setParentProject(project);
			project.getProjectTeam().add(member);
			session.save(project);

			transaction.commit();

		} catch ( RuntimeException rte ) {
			ExceptionHandler.handleException(rte, new ExceptionEvent(ExceptionEvent.POJOSAMPLES));
			if ( transaction != null ) {
				if ( transaction.isActive() ) {
					try {
						transaction.rollback();
					} catch ( HibernateException hibernateException ) {
						ExceptionHandler.handleException(hibernateException, new ExceptionEvent(ExceptionEvent.POJOSAMPLES));
					}
				}
			}
		}

	}
}
